﻿// (c) Mihail Kuznesov, Ashgabat, Turkmenistan
// 
using System;
using System.Collections.Generic;
using System.Text;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;


namespace text2pdf
{

    class Program
    {

        static string BadCommandLine = @"text2pdf ""c:\filepath\filenameTemplate.pdf"" ""c:\filepath\filenameOutput.pdf"" Page PositionX PositionY Width Height Font FontSize ""Text To Include\nSecond line\nNext Line "" ";

        static string CommandExample = @"text2pdf ""c:\1\123.pdf"" ""c:\1\out.pdf"" 1 50 200 200 200 ""Verdana"" 12 ""Text To Include\nSecond line\nNext Line "" ";
        static void Main(string[] args)
        { try
            {

                Console.WriteLine("(c) Mihail Kuznesov, Ashgabat. Turkmenistan. 2017");
                Console.WriteLine("");

                if (args.Length != 10)
                {
                    Console.WriteLine(BadCommandLine);
                    Console.WriteLine();
                    Console.WriteLine(CommandExample);

                    
                    Environment.Exit(1);

                }
                string inFile = args[0];
                string outFile = args[1];
                int Page = 0;
                if (int.TryParse(args[2], out Page) != true) { Console.WriteLine("Can not parse argument Page"); Environment.Exit(1); }
                // PositionX PositionY LengthX LengthY Font FontSize
                int PositionX = 0;
                if (int.TryParse(args[3], out PositionX) != true) { Console.WriteLine("Can not parse argument PositionX"); Environment.Exit(1); }
                int PositionY = 0;
                if (int.TryParse(args[4], out PositionY) != true) { Console.WriteLine("Can not parse argument PositionY"); Environment.Exit(1); }
                int Width = 0;
                if (int.TryParse(args[5], out Width) != true) { Console.WriteLine("Can not parse argument Width"); Environment.Exit(1); }
                int Height = 0;
                if (int.TryParse(args[6], out Height) != true) { Console.WriteLine("Can not parse argument Height"); Environment.Exit(1); }
                string FontName = args[7];
                int FontSize = 0;
                if (int.TryParse(args[8], out FontSize) != true) { Console.WriteLine("Can not parse argument FontSize"); Environment.Exit(1); }
                String text = args[9];

                text = text.Replace(@"\n", "\n");

                PdfSharp.Pdf.PdfDocument PDFDoc = PdfSharp.Pdf.IO.PdfReader.Open(inFile, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Import);
                PdfSharp.Pdf.PdfDocument PDFNewDoc = new PdfSharp.Pdf.PdfDocument();

                for (int Pg = 0; Pg < PDFDoc.Pages.Count; Pg++)
                {
                    PDFNewDoc.AddPage(PDFDoc.Pages[Pg]);
                    PdfSharp.Pdf.PdfPage page = PDFNewDoc.Pages[Pg];

                    if (Pg == (Page - 1))
                    {
                        XGraphics gfx = XGraphics.FromPdfPage(page);
                        XFont font = new XFont(FontName, FontSize, XFontStyle.Regular);
                        PdfSharp.Drawing.Layout.XTextFormatter tf = new PdfSharp.Drawing.Layout.XTextFormatter(gfx);
                        XRect rect = new XRect(PositionX, PositionY, Width, Height);
                        tf.DrawString(text, font, XBrushes.Black, rect, XStringFormats.TopLeft);
                    }

                }
                PDFNewDoc.Save(outFile);
                Environment.Exit(0);
            } catch
            {
                Console.WriteLine("");
                Console.WriteLine("Error");
                Environment.Exit(1);
            }
        }
    }
    
}
